<template>
	<view class="page_wrapper page_bg">
		<Navbar title="数的分解" />
		<view class="pagebox">
			<view class="first_row">
				<view class="num_limit_block">
					<view class="num_limit" :class="{'active' : maxNum === 5}" @tap="changeLimit(5)">5以内</view>
					<view class="num_limit" :class="{'active' : maxNum === 9}" @tap="changeLimit(9)">10以内</view>
				</view>
				<view class="right_block">
					<u-checkbox usedAlone activeColor="#2E996C" @change="showTip()" labelColor="#2E996C" labelSize="32rpx" v-model:checked="tipFlag" label="提示" shape="circle"></u-checkbox>
				</view>
			</view>
			<view class="first_block">
				<view class="numbox">
					<view class="second_row" v-if="tipFlag">
						<view class="shape" v-for="(num, index) in mergeNum" :key="index">
							<block v-if="num <= leftNum">
								<view class="shape_box select_item"></view>
							</block>
							<block v-else>
								<view class="shape_box"></view>
							</block>
						</view>
					</view>
					<view class="three_row" :style="!tipFlag?'margin-top:100rpx':''">
						<view class="num_input merge_num">
							<block v-if="mergeNum >= 0">{{mergeNum}}</block>
						</view>
					</view>
					<view class="four_row">
						<view class="first_line"></view>
					</view>
					<view class="five_row">
						<view class="num_input first_num" :class="{'inputing': hideNum === false}">
							<block v-if="leftNum >= 0 && hideNum">{{leftNum}}</block>
							<block v-if="hideNum === false && userInput !== ''">{{userInput}}</block>
						</view>
						<view class="num_input second_num" :class="{'inputing': hideNum}">
							<block v-if="rightNum >= 0 && hideNum === false">{{rightNum}}</block>
							<block v-if="hideNum === true && userInput !== ''">{{userInput}}</block>
						</view>
					</view>
				</view>
				<image class="test_bg" src="https://www.hzif.cn/mathematics-attach/test_bg.png" mode=""></image>
			</view>
			<view class="keyboard_block">
				<view class="keyboard_wrapper" v-for="(key, index) in 10" :key="index">
					<view class="key" @tap="keyDown(key%10)">{{key%10}}</view>
				</view>
			</view>
		</view>
		<view class="btns_block">
			<button class="share_btn" open-type="share">分享给好友</button>
		</view>
		<image src="../../static/nimg/bg.png" class="page_bg_img" mode=""></image>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js'
	import {
		utils
	} from '@/utils/utils.js'
	import {
		storageData
	} from '@/utils/storageData.js'
	import Navbar from '@/components/navbar.vue'
	export default {
		components: {
			Navbar
		},
		data() {
			return {
				tipFlag: true,
				userInput: '',
				mergeNum: 0,
				leftNum: -1,
				rightNum: -1,
				maxNum: 5,
				hideNum: false,
			}
		},
		onLoad() {
			this.nextNum()
		},
		methods: {
			changeLimit(maxNum) {
				this.maxNum = maxNum
				this.nextNum()
			},
			nextNum() {
				// false 隐藏左侧， true隐藏右侧
				this.userInput = ''
				this.hideNum = this.getRandomBoolean()
				console.log(this.hideNum)
				this.mergeNum = this.getRandomNumber(this.maxNum)
				this.leftNum = this.getRandomNumber(this.mergeNum)
				this.rightNum = this.mergeNum - this.leftNum
				console.log(this.mergeNum,this.leftNum,this.rightNum);

			},
			getRandomNumber(maxNum) {
				return Math.floor(Math.random() * maxNum) + 1;
			},
			getSecondRandomNumber(maxNum) {
				return Math.floor(Math.random() * maxNum);
			},
			getRandomBoolean() {
				return Math.random() >= 0.5;
			},
			showTip() {
				this.tipFlag = !this.tipFlag
			},
			keyDown(num) {
				// 比较结果
				let result = ''
				if (this.hideNum === false) {
					result = this.leftNum === num ? '正确' : '错误'
				} else {
					result = this.rightNum === num ? '正确' : '错误'
				}
				uni.showToast({
					icon: 'none',
					title: result,
					duration: 1000
				})
				if (result === '错误') {
					return false
				}
				this.userInput = num
				const $this = this
				setTimeout(() => {
					$this.nextNum()
				}, 500)
			}
		},
		onShareAppMessage(res) {
			const userInfo = storageData.getLoginUser()
			if (res.from === 'button') {
				// 来自页面内分享按钮
				console.log(res.target);
			}
			return {
				title: '小奕口算：数的分解',
				path: '/pages/numFenJie/numFenJie?shareUc=' + userInfo.userCode,
				mpId: 'wxcbbe1a868c5c3c3c', //此处配置微信小程序的AppId
			}
		},
		onShareTimeline(res) {
			const userInfo = storageData.getLoginUser()
			return {
				title: '小奕口算：数的分解',
				path: '/pages/numFenJie/numFenJie?shareUc=' + userInfo.userCode,
				mpId: 'wxcbbe1a868c5c3c3c', //此处配置微信小程序的AppId
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page_wrapper {
		.first_row {
			padding: 10px 10px 0px 10px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 10px;

			.left_block,
			.right_block {
				flex: 1;
				display: flex;
				justify-content: flex-end;
				align-items: center;
			}

			.num_limit_block {
				width: 260rpx;
				height: 80rpx;
				display: flex;
				justify-content: center;
				background-color: #C4E6C2;
				align-items: center;
				border-radius:40rpx;
				.num_limit {
					border-radius: 40rpx;
					width: 150rpx;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #2E996C;
					font-size:32rpx;
					font-weight: 400;
				}

				.active {
					color: white;
					background-color: #2E996C;
				}
			}
		}
		.first_block {
			display: flex;
			flex-direction: column;
			position: relative;
			.numbox{
				top: 40rpx;
				position: absolute;
				width: 100%;
				color: #ffffff;
			}
			.test_bg{
				height: 820rpx;
				width: 100%;
			}
			.num_input {
				width:200rpx;
				height: 148rpx;
				font-size: 100rpx;
				font-family: eryaxinhei;
				background: #328372;
				border: 4rpx solid #C4E6C2;
				border-radius: 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.inputing {
				background: rgba(235,156,0,0.42);
				border-radius: 20rpx;
				border: 4rpx solid #EA8742;
			}

			.second_row {
				height: 100px;
				padding: 10px 20px 10px 20px;
				display: flex;
				flex-wrap: wrap;
				.shape {
					width: 20%;
					margin-bottom: 10px;
					display: flex;
					justify-content: center;
					.shape_box{
						width: 60rpx;
						height: 60rpx;
						background-color:#C4E6C2;
					}
					.select_item {
						background-color: #FF8835;
						// FF8835
						// border-bottom: 24px solid green !important;
					}
				}
			}

			.three_row,
			.four_row,
			.five_row {
				display: flex;
				justify-content: center;
			}

			.four_row {
				height: 50px;
				padding-top: 5px;
				padding-bottom: 5px;

				.first_line {
					width: 70px;
					/* 根据需要设置宽度 */
					height: 70px;
					/* 高度为1px以实现线的效果 */
					background: transparent;
					/* 背景透明 */
					border-top: 4rpx solid #C4E6C2;
					/* 上边框为斜线 */
					border-right: 4rpx solid #C4E6C2;
					/* 上边框为斜线 */
					transform: rotate(-45deg);
					/* 旋转45度以创建斜线 */
					// background-color: red;
					margin-top: 13px;
				}

			}

			.five_row {
				.first_num {
					margin-right: 50px;
				}

				margin-bottom: 20px;
			}
		}

		.keyboard_block {
			margin-top: 40rpx;
			display: flex;
			flex-wrap: wrap;
			.keyboard_wrapper {
				width: 20%;
				height: 94rpx;
				margin-bottom: 5px;
				color: #fff;
				.key {
					width: 95%;
					height: 94rpx;
					background-color: #36ac7a;
					border-radius: 10rpx;
					font-size: 66rpx;
					font-family: eryaxinhei;
					box-shadow: inset 0 0 5rpx 6rpx #56ce9b;
					text-shadow: -4rpx 1rpx 10rpx #00000055;
					display: flex;
					align-items: center;
					justify-content: center;
				}
				.key:active {
					background-color: #2A6C5E;
				}
			}
		}

		.btns_block {
			position: fixed;
			bottom: 100rpx;
			z-index: 4;
			width: 100%;

			.share_btn {
				font-size: 14px;
				width: 80%;
				height: 98rpx;
				color: white;
				background-color: #2E996C;
				border-radius: 50px;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
	:deep(.u-checkbox .u-icon text){
		color: #00000000 !important;
		background-color: #C4E6C2;
		border-radius: 50%;
		font-size: 16rpx !important;
		line-height: 16rpx !important;
	}
</style>